package com.hst.capacity.infra.persistence.jpa.setting.line;

import com.hst.capacity.domain.model.entity.setting.line.CityLineEntity;
import com.hst.capacity.domain.model.response.setting.line.CityLineInfoVO;
import net.bytebuddy.build.HashCodeAndEqualsPlugin;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface CityLineRepository extends JpaRepository<CityLineEntity, String> {


    CityLineEntity findByIdCityLine(String idCityLine);

    List<CityLineEntity> findByIdFranchisee(String idFranchisee);
    /**
     * 通过地区CODE查找运营城际线路
     * @param cityCode
     * @return
     */
    @Query("select t from CityLineEntity t where t.cityCode=:cityCode and t.status =1 order by t.sort desc")
    List<CityLineEntity> findByCityCode(@Param("cityCode")String cityCode);

    /**
     * 通过地区CODE查找运营城际线路
     * @param cityCode
     * @return
     */
    @Query(value = "select new com.hst.capacity.domain.model.response.setting.line.CityLineInfoVO(t) from CityLineEntity t where t.cityCode=:cityCode order by t.sort desc",
            countQuery = "select count(t.idCityLine) from CityLineEntity t where t.cityCode=:cityCode order by t.sort desc")
    Page<CityLineInfoVO> findCityLineByCityCode(@Param("cityCode")String cityCode, Pageable pageable);

    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update CityLineEntity t set t.status = ?1 where t.idCityLine = ?2")
    int updateCityLineStatus(int status, String idCityLine);

}